<template>
  <div v-if="moduleNameOptions && form.type">
    <el-dialog
      :title="
        (mode === 'add' ? '新增' : '编辑') +
          moduleNameOptions[form.type].label +
          '内容'
      "
      :visible.sync="dialogVisible"
      :append-to-body="true"
      :close-on-click-modal="false"
      width="750px"
      @close="close"
    >
      <el-form ref="form" v-loading="loading" :model="form" :rules="rules" label-width="100px" label-suffix="：" size="small">
        <el-form-item label="标题" prop="title">
          <el-input v-model.trim="form.title" maxlength="30" show-word-limit />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="form.content" :autosize="{ minRows: 2, maxRows: 10 }" type="textarea" maxlength="256" show-word-limit />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button size="small" round @click="hide">取 消</el-button>
        <el-button size="small" round type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import wxaBuildConfig from '@/views/wxaBuildModule/config'
export default {
  data() {
    return {
      // 表单校验相关
      rules: {
        title: [
          {
            required: true,
            message: '请输入标题',
            trigger: ['blur', 'change']
          }
        ],
        content: [
          {
            required: true,
            message: '请输入内容',
            trigger: ['blur', 'change']
          }
        ]
      },
      // 配置
      resolve: null,
      reject: null,
      moduleNameOptions: null,
      // 状态
      loading: false,
      dialogVisible: null,
      // 数据
      classifyCode: 'DEFAULT',
      mode: 'add',
      form: {
        bg: '',
        biz: {
          params: '',
          type: ''
        },
        color: '',
        content: '',
        dump: {
          appId: '',
          feature: '',
          featureParams: '',
          insidePage: '',
          insideParams: '',
          path: '',
          type: '',
          url: ''
        },
        modules: [],
        pic: '',
        ranking: 0,
        style: '',
        subTitle: '',
        subType: '',
        title: '',
        type: ''
      }
    }
  },
  watch: {},
  computed: {
    ...mapGetters(['fileBaseUrl', 'activePageForm'])
  },
  mounted() {
    this.moduleNameOptions = wxaBuildConfig[this.classifyCode].moduleName
  },
  methods: {
    /**
       * 弹框相关
       */
    open({ mode = 'add', row = '' }) {
      this.mode = mode
      if (mode === 'edit' && row) {
        this.form = JSON.parse(JSON.stringify(row))
      } else {
        this.defaultForm()
      }
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    defaultForm() {
      this.form = {
        bg: '',
        biz: {
          params: '',
          type: this.activePageForm.modules[
            this.activePageForm.activeModuleIndex
          ].biz.type
        },
        color: '',
        content: '',
        dump: {
          appId: '',
          feature: '',
          featureParams: '',
          insidePage: '',
          insideParams: '',
          path: '',
          type: '',
          url: ''
        },
        modules: [],
        pic: '',
        ranking: 0,
        style: '',
        subTitle: '',
        subType: '',
        title: '',
        type: this.activePageForm.modules[this.activePageForm.activeModuleIndex]
          .type
      }
    },

    /**
       * 数据组
       */

    /**
       * 按钮组
       */
    // 确 定
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.form))
          this.resolve && this.resolve(params)
          this.hide()
        }
      })
    }

    /**
       * 状态组
       */

    /**
       * 方法组
       */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .w-400 {
    width: 400px;
  }
</style>
